home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / print.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  29KB  |  1,087 lines

  1. /* Lisp object printing and output streams.
  2.    Copyright (C) 1985, 1986, 1988, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. #undef NULL
  24. #include "lisp.h"
  25.  
  26. #ifndef standalone
  27. #include "buffer.h"
  28. #include "frame.h"
  29. #include "window.h"
  30. #include "process.h"
  31. #include "dispextern.h"
  32. #include "termchar.h"
  33. #endif /* not standalone */
  34.  
  35. #ifdef USE_TEXT_PROPERTIES
  36. #include "intervals.h"
  37. #endif
  38.  
  39. #include "print_p.h"
  40. #include "insdel_p.h"
  41. static void printchar _P_((unsigned char ch, Lisp_Object fun));
  42. static void strout _P_((char *ptr, int size, Lisp_Object printcharfun));
  43. static void print _P_((Lisp_Object obj, register Lisp_Object printcharfun,
  44.                        int escapeflag));
  45.  
  46. Lisp_Object Vstandard_output, Qstandard_output;
  47.  
  48. #ifdef LISP_FLOAT_TYPE
  49. Lisp_Object Vfloat_output_format, Qfloat_output_format;
  50. #endif /* LISP_FLOAT_TYPE */
  51.  
  52. /* Avoid actual stack overflow in print.  */
  53. int print_depth;
  54.  
  55. /* Detect most circularities to print finite output.  */
  56. #define PRINT_CIRCLE 200
  57. Lisp_Object being_printed[PRINT_CIRCLE];
  58.  
  59. /* Maximum length of list to print in full; noninteger means
  60.    effectively infinity */
  61.  
  62. Lisp_Object Vprint_length;
  63.  
  64. /* Maximum depth of list to print in full; noninteger means
  65.    effectively infinity.  */
  66.  
  67. Lisp_Object Vprint_level;
  68.  
  69. /* Nonzero means print newlines in strings as \n.  */
  70.  
  71. int print_escape_newlines;
  72.  
  73. Lisp_Object Qprint_escape_newlines;
  74.  
  75. /* Nonzero means print newline before next minibuffer message.
  76.    Defined in xdisp.c */
  77.  
  78. extern int noninteractive_need_newline;
  79. #ifdef MAX_PRINT_CHARS
  80. static int print_chars;
  81. static int max_print;
  82. #endif /* MAX_PRINT_CHARS */
  83.  
  84.  
  85. #if 0
  86. /* Convert between chars and GLYPHs */
  87.  
  88. int
  89. glyphlen (glyphs)
  90.      register GLYPH *glyphs;
  91. {
  92.   register int i = 0;
  93.  
  94.   while (glyphs[i])
  95.     i++;
  96.   return i;
  97. }
  98.  
  99. void
  100. str_to_glyph_cpy (str, glyphs)
  101.      char *str;
  102.      GLYPH *glyphs;
  103. {
  104.   register GLYPH *gp = glyphs;
  105.   register char *cp = str;
  106.  
  107.   while (*cp)
  108.     *gp++ = *cp++;
  109. }
  110.  
  111. void
  112. str_to_glyph_ncpy (str, glyphs, n)
  113.      char *str;
  114.      GLYPH *glyphs;
  115.      register int n;
  116. {
  117.   register GLYPH *gp = glyphs;
  118.   register char *cp = str;
  119.  
  120.   while (n-- > 0)
  121.     *gp++ = *cp++;
  122. }
  123.  
  124. void
  125. glyph_to_str_cpy (glyphs, str)
  126.      GLYPH *glyphs;
  127.      char *str;
  128. {
  129.   register GLYPH *gp = glyphs;
  130.   register char *cp = str;
  131.  
  132.   while (*gp)
  133.     *str++ = *gp++ & 0377;
  134. }
  135. #endif
  136.  
  137. /* Low level output routines for characters and strings */
  138.  
  139. /* Lisp functions to do output using a stream
  140.  must have the stream in a variable called printcharfun
  141.  and must start with PRINTPREPARE and end with PRINTFINISH.
  142.  Use PRINTCHAR to output one character,
  143.  or call strout to output a block of characters.
  144.  Also, each one must have the declarations
  145.    struct buffer *old = current_buffer;
  146.    int old_point = -1, start_point;
  147.    Lisp_Object original;
  148. */ 
  149.  
  150. #define PRINTPREPARE                        \
  151.    original = printcharfun;                    \
  152.    if (NILP (printcharfun)) printcharfun = Qt;            \
  153.    if (XTYPE (printcharfun) == Lisp_Buffer)            \
  154.      { if (XBUFFER (printcharfun) != current_buffer)        \
  155.      Fset_buffer (printcharfun);                \
  156.        printcharfun = Qnil;}                    \
  157.    if (XTYPE (printcharfun) == Lisp_Marker)            \
  158.      { if (!(XMARKER (original)->buffer))            \
  159.          error ("Marker does not point anywhere");        \
  160.        if (XMARKER (original)->buffer != current_buffer)    \
  161.          set_buffer_internal (XMARKER (original)->buffer);    \
  162.        old_point = point;                    \
  163.        SET_PT (marker_position (printcharfun));            \
  164.        start_point = point;                    \
  165.        printcharfun = Qnil;}
  166.  
  167. #define PRINTFINISH                    \
  168.    if (XTYPE (original) == Lisp_Marker)            \
  169.      Fset_marker (original, make_number (point), Qnil);    \
  170.    if (old_point >= 0)                    \
  171.      SET_PT (old_point + (old_point >= start_point    \
  172.               ? point - start_point : 0));    \
  173.    if (old != current_buffer)                \
  174.      set_buffer_internal (old)
  175.  
  176. #define PRINTCHAR(ch) printchar (ch, printcharfun)
  177.  
  178. /* Index of first unused element of FRAME_MESSAGE_BUF(selected_frame). */
  179. static int printbufidx;
  180.  
  181. static void
  182. printchar (ch, fun)
  183.      unsigned char ch;
  184.      Lisp_Object fun;
  185. {
  186.   Lisp_Object ch1;
  187.  
  188. #ifdef MAX_PRINT_CHARS
  189.   if (max_print)
  190.     print_chars++;
  191. #endif /* MAX_PRINT_CHARS */
  192. #ifndef standalone
  193.   if (EQ (fun, Qnil))
  194.     {
  195.       QUIT;
  196.       insert (&ch, 1);
  197.       return;
  198.     }
  199.  
  200.   if (EQ (fun, Qt))
  201.     {
  202.       if (noninteractive)
  203.     {
  204.       putchar (ch);
  205.       noninteractive_need_newline = 1;
  206.       return;
  207.     }
  208.  
  209.       if (echo_area_glyphs != FRAME_MESSAGE_BUF (selected_frame)
  210.       || !message_buf_print)
  211.     {
  212.       echo_area_glyphs = FRAME_MESSAGE_BUF (selected_frame);
  213.       printbufidx = 0;
  214.       message_buf_print = 1;
  215.     }
  216.  
  217.       if (printbufidx < FRAME_WIDTH (selected_frame) - 1)
  218.     FRAME_MESSAGE_BUF (selected_frame)[printbufidx++] = ch;
  219.       FRAME_MESSAGE_BUF (selected_frame)[printbufidx] = 0;
  220.  
  221.       return;
  222.     }
  223. #endif /* not standalone */
  224.  
  225.   XFASTINT (ch1) = ch;
  226.   call1 (fun, ch1);
  227. }
  228.  
  229. static void
  230. strout (ptr, size, printcharfun)
  231.      char *ptr;
  232.      int size;
  233.      Lisp_Object printcharfun;
  234. {
  235.   int i = 0;
  236.  
  237.   if (EQ (printcharfun, Qnil))
  238.     {
  239.       insert (ptr, size >= 0 ? size : strlen (ptr));
  240. #ifdef MAX_PRINT_CHARS
  241.       if (max_print)
  242.         print_chars += size >= 0 ? size : strlen(ptr);
  243. #endif /* MAX_PRINT_CHARS */
  244.       return;
  245.     }
  246.   if (EQ (printcharfun, Qt))
  247.     {
  248.       i = size >= 0 ? size : strlen (ptr);
  249. #ifdef MAX_PRINT_CHARS
  250.       if (max_print)
  251.         print_chars += i;
  252. #endif /* MAX_PRINT_CHARS */
  253.  
  254.       if (noninteractive)
  255.     {
  256.       fwrite (ptr, 1, i, stdout);
  257.       noninteractive_need_newline = 1;
  258.       return;
  259.     }
  260.  
  261.       if (echo_area_glyphs != FRAME_MESSAGE_BUF (selected_frame)
  262.       || !message_buf_print)
  263.     {
  264.       echo_area_glyphs = FRAME_MESSAGE_BUF (selected_frame);
  265.       printbufidx = 0;
  266.       message_buf_print = 1;
  267.     }
  268.  
  269.       if (i > FRAME_WIDTH (selected_frame) - printbufidx - 1)
  270.     i = FRAME_WIDTH (selected_frame) - printbufidx - 1;
  271.       bcopy (ptr, &FRAME_MESSAGE_BUF (selected_frame) [printbufidx], i);
  272.       printbufidx += i;
  273.       FRAME_MESSAGE_BUF (selected_frame) [printbufidx] = 0;
  274.  
  275.       return;
  276.     }
  277.  
  278.   if (size >= 0)
  279.     while (i < size)
  280.       PRINTCHAR (ptr[i++]);
  281.   else
  282.     while (ptr[i])
  283.       PRINTCHAR (ptr[i++]);
  284. }
  285.  
  286. /* Print the contents of a string STRING using PRINTCHARFUN.
  287.    It isn't safe to use strout, because printing one char can relocate.  */
  288.  
  289. _VOID_
  290. print_string (string, printcharfun)
  291.      Lisp_Object string;
  292.      Lisp_Object printcharfun;
  293. {
  294.   if (EQ (printcharfun, Qnil) || EQ (printcharfun, Qt))
  295.     /* In predictable cases, strout is safe: output to buffer or frame.  */
  296.     strout (XSTRING (string)->data, XSTRING (string)->size, printcharfun);
  297.   else
  298.     {
  299.       /* Otherwise, fetch the string address for each character.  */
  300.       int i;
  301.       int size = XSTRING (string)->size;
  302.       struct gcpro gcpro1;
  303.       GCPRO1 (string);
  304.       for (i = 0; i < size; i++)
  305.     PRINTCHAR (XSTRING (string)->data[i]);
  306.       UNGCPRO;
  307.     }
  308. }
  309.  
  310. DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
  311.   "Output character CHAR to stream STREAM.\n\
  312. STREAM defaults to the value of `standard-output' (which see).")
  313.   (ch, printcharfun)
  314.      Lisp_Object ch, printcharfun;
  315. {
  316.   struct buffer *old = current_buffer;
  317.   int old_point = -1;
  318.   int start_point;
  319.   Lisp_Object original;
  320.  
  321.   if (NILP (printcharfun))
  322.     printcharfun = Vstandard_output;
  323.   CHECK_NUMBER (ch, 0);
  324.   PRINTPREPARE;
  325.   PRINTCHAR ((char)(XINT (ch)));
  326.   PRINTFINISH;
  327.   return ch;
  328. }
  329.  
  330. /* Used from outside of print.c to print a block of SIZE chars at DATA
  331.    on the default output stream.
  332.    Do not use this on the contents of a Lisp string.  */
  333.  
  334. _VOID_
  335. write_string (data, size)
  336.      char *data;
  337.      int size;
  338. {
  339.   struct buffer *old = current_buffer;
  340.   Lisp_Object printcharfun;
  341.   int old_point = -1;
  342.   int start_point;
  343.   Lisp_Object original;
  344.  
  345.   printcharfun = Vstandard_output;
  346.  
  347.   PRINTPREPARE;
  348.   strout (data, size, printcharfun);
  349.   PRINTFINISH;
  350. }
  351.  
  352. /* Used from outside of print.c to print a block of SIZE chars at DATA
  353.    on a specified stream PRINTCHARFUN.
  354.    Do not use this on the contents of a Lisp string.  */
  355.  
  356. _VOID_
  357. write_string_1 (data, size, printcharfun)
  358.      char *data;
  359.      int size;
  360.      Lisp_Object printcharfun;
  361. {
  362.   struct buffer *old = current_buffer;
  363.   int old_point = -1;
  364.   int start_point;
  365.   Lisp_Object original;
  366.  
  367.   PRINTPREPARE;
  368.   strout (data, size, printcharfun);
  369.   PRINTFINISH;
  370. }
  371.  
  372.  
  373. #ifndef standalone
  374.  
  375. void
  376. temp_output_buffer_setup (bufname)
  377.     char *bufname;
  378. {
  379.   register struct buffer *old = current_buffer;
  380.   register Lisp_Object buf;
  381.  
  382.   Fset_buffer (Fget_buffer_create (build_string (bufname)));
  383.  
  384.   current_buffer->read_only = Qnil;
  385.   Ferase_buffer ();
  386.  
  387.   XSET (buf, Lisp_Buffer, current_buffer);
  388.   specbind (Qstandard_output, buf);
  389.  
  390.   set_buffer_internal (old);
  391. }
  392.  
  393. Lisp_Object
  394. internal_with_output_to_temp_buffer (bufname, function, args)
  395.      char *bufname;
  396.      Lisp_Object (*function) _P_((Lisp_Object args));
  397.      Lisp_Object args;
  398. {
  399.   int count = specpdl_ptr - specpdl;
  400.   Lisp_Object buf, val;
  401.  
  402.   record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  403.   temp_output_buffer_setup (bufname);
  404.   buf = Vstandard_output;
  405.  
  406.   val = (*function) (args);
  407.  
  408.   temp_output_buffer_show (buf);
  409.  
  410.   return unbind_to (count, val);
  411. }
  412.  
  413. DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
  414.        1, UNEVALLED, 0,
  415.   "Bind `standard-output' to buffer BUFNAME, eval BODY, then show that buffer.\n\
  416. The buffer is cleared out initially, and marked as unmodified when done.\n\
  417. All output done by BODY is inserted in that buffer by default.\n\
  418. The buffer is displayed in another window, but not selected.\n\
  419. The value of the last form in BODY is returned.\n\
  420. If BODY does not finish normally, the buffer BUFNAME is not displayed.\n\n\
  421. If variable `temp-buffer-show-function' is non-nil, call it at the end\n\
  422. to get the buffer displayed.  It gets one argument, the buffer to display.")
  423.   (args)
  424.      Lisp_Object args;
  425. {
  426.   struct gcpro gcpro1;
  427.   Lisp_Object name;
  428.   int count = specpdl_ptr - specpdl;
  429.   Lisp_Object buf, val;
  430.  
  431.   GCPRO1(args);
  432.   name = Feval (Fcar (args));
  433.   UNGCPRO;
  434.  
  435.   CHECK_STRING (name, 0);
  436.   temp_output_buffer_setup (XSTRING (name)->data);
  437.   buf = Vstandard_output;
  438.  
  439.   val = Fprogn (Fcdr (args));
  440.  
  441.   temp_output_buffer_show (buf);
  442.  
  443.   return unbind_to (count, val);
  444. }
  445. #endif /* not standalone */
  446.  
  447.  
  448. DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
  449.   "Output a newline to STREAM.\n\
  450. If STREAM is omitted or nil, the value of `standard-output' is used.")
  451.   (printcharfun)
  452.      Lisp_Object printcharfun;
  453. {
  454.   struct buffer *old = current_buffer;
  455.   int old_point = -1;
  456.   int start_point;
  457.   Lisp_Object original;
  458.  
  459.   if (NILP (printcharfun))
  460.     printcharfun = Vstandard_output;
  461.   PRINTPREPARE;
  462.   PRINTCHAR ('\n');
  463.   PRINTFINISH;
  464.   return Qt;
  465. }
  466.  
  467. DEFUN ("prin1", Fprin1, Sprin1, 1, 2, 0,
  468.   "Output the printed representation of OBJECT, any Lisp object.\n\
  469. Quoting characters are printed when needed to make output that `read'\n\
  470. can handle, whenever this is possible.\n\
  471. Output stream is STREAM, or value of `standard-output' (which see).")
  472.   (obj, printcharfun)
  473.      Lisp_Object obj, printcharfun;
  474. {
  475.   struct buffer *old = current_buffer;
  476.   int old_point = -1;
  477.   int start_point;
  478.   Lisp_Object original;
  479.  
  480. #ifdef MAX_PRINT_CHARS
  481.   max_print = 0;
  482. #endif /* MAX_PRINT_CHARS */
  483.   if (NILP (printcharfun))
  484.     printcharfun = Vstandard_output;
  485.   PRINTPREPARE;
  486.   print_depth = 0;
  487.   print (obj, printcharfun, 1);
  488.   PRINTFINISH;
  489.   return obj;
  490. }
  491.  
  492. /* a buffer which is used to hold output being built by prin1-to-string */
  493. Lisp_Object Vprin1_to_string_buffer;
  494.  
  495. DEFUN ("prin1-to-string", Fprin1_to_string, Sprin1_to_string, 1, 2, 0,
  496.   "Return a string containing the printed representation of OBJECT,\n\
  497. any Lisp object.  Quoting characters are used when needed to make output\n\
  498. that `read' can handle, whenever this is possible, unless the optional\n\
  499. second argument NOESCAPE is non-nil.")
  500.   (obj, noescape)
  501.      Lisp_Object obj, noescape;
  502. {
  503.   struct buffer *old = current_buffer;
  504.   int old_point = -1;
  505.   int start_point;
  506.   Lisp_Object original, printcharfun;
  507.   struct gcpro gcpro1;
  508.  
  509.   printcharfun = Vprin1_to_string_buffer;
  510.   PRINTPREPARE;
  511.   print_depth = 0;
  512.   print (obj, printcharfun, NILP (noescape));
  513.   /* Make Vprin1_to_string_buffer be the default buffer after PRINTFINSH */
  514.   PRINTFINISH;
  515.   set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  516.   obj = Fbuffer_string ();
  517.  
  518.   GCPRO1 (obj);
  519.   Ferase_buffer ();
  520.   set_buffer_internal (old);
  521.   UNGCPRO;
  522.  
  523.   return obj;
  524. }
  525.  
  526. DEFUN ("princ", Fprinc, Sprinc, 1, 2, 0,
  527.   "Output the printed representation of OBJECT, any Lisp object.\n\
  528. No quoting characters are used; no delimiters are printed around\n\
  529. the contents of strings.\n\
  530. Output stream is STREAM, or value of standard-output (which see).")
  531.   (obj, printcharfun)
  532.      Lisp_Object obj, printcharfun;
  533. {
  534.   struct buffer *old = current_buffer;
  535.   int old_point = -1;
  536.   int start_point;
  537.   Lisp_Object original;
  538.  
  539.   if (NILP (printcharfun))
  540.     printcharfun = Vstandard_output;
  541.   PRINTPREPARE;
  542.   print_depth = 0;
  543.   print (obj, printcharfun, 0);
  544.   PRINTFINISH;
  545.   return obj;
  546. }
  547.  
  548. DEFUN ("print", Fprint, Sprint, 1, 2, 0,
  549.   "Output the printed representation of OBJECT, with newlines around it.\n\
  550. Quoting characters are printed when needed to make output that `read'\n\
  551. can handle, whenever this is possible.\n\
  552. Output stream is STREAM, or value of `standard-output' (which see).")
  553.   (obj, printcharfun)
  554.      Lisp_Object obj, printcharfun;
  555. {
  556.   struct buffer *old = current_buffer;
  557.   int old_point = -1;
  558.   int start_point;
  559.   Lisp_Object original;
  560.   struct gcpro gcpro1;
  561.  
  562. #ifdef MAX_PRINT_CHARS
  563.   print_chars = 0;
  564.   max_print = MAX_PRINT_CHARS;
  565. #endif /* MAX_PRINT_CHARS */
  566.   if (NILP (printcharfun))
  567.     printcharfun = Vstandard_output;
  568.   GCPRO1 (obj);
  569.   PRINTPREPARE;
  570.   print_depth = 0;
  571.   PRINTCHAR ('\n');
  572.   print (obj, printcharfun, 1);
  573.   PRINTCHAR ('\n');
  574.   PRINTFINISH;
  575. #ifdef MAX_PRINT_CHARS
  576.   max_print = 0;
  577.   print_chars = 0;
  578. #endif /* MAX_PRINT_CHARS */
  579.   UNGCPRO;
  580.   return obj;
  581. }
  582.  
  583. /* The subroutine object for external-debugging-output is kept here
  584.    for the convenience of the debugger.  */
  585. Lisp_Object Qexternal_debugging_output;
  586.  
  587. DEFUN ("external-debugging-output", Fexternal_debugging_output, Sexternal_debugging_output, 1, 1, 0,
  588.   "Write CHARACTER to stderr.\n\
  589. You can call print while debugging emacs, and pass it this function\n\
  590. to make it write to the debugging output.\n")
  591.   (character)
  592.      Lisp_Object character;
  593. {
  594.   CHECK_NUMBER (character, 0);
  595.   putc (XINT (character), stderr);
  596.   
  597.   return character;
  598. }
  599.  
  600. #ifdef LISP_FLOAT_TYPE
  601.  
  602. /*
  603.  * The buffer should be at least as large as the max string size of the
  604.  * largest float, printed in the biggest notation.  This is undoubtably
  605.  * 20d float_output_format, with the negative of the C-constant "HUGE"
  606.  * from <math.h>.
  607.  * 
  608.  * On the vax the worst case is -1e38 in 20d format which takes 61 bytes.
  609.  * 
  610.  * I assume that IEEE-754 format numbers can take 329 bytes for the worst
  611.  * case of -1e307 in 20d float_output_format. What is one to do (short of
  612.  * re-writing _doprnt to be more sane)?
  613.  *             -wsr
  614.  */
  615.  
  616. void
  617. float_to_string (buf, data)
  618.      unsigned char *buf;
  619.      double data;
  620. {
  621.   unsigned char *cp;
  622.   int width = -1;
  623.       
  624.   if (NILP (Vfloat_output_format)
  625.       || XTYPE (Vfloat_output_format) != Lisp_String)
  626.   lose:
  627.     sprintf (buf, "%.20g", data);
  628.   else            /* oink oink */
  629.     {
  630.       /* Check that the spec we have is fully valid.
  631.      This means not only valid for printf,
  632.      but meant for floats, and reasonable.  */
  633.       cp = XSTRING (Vfloat_output_format)->data;
  634.  
  635.       if (cp[0] != '%')
  636.     goto lose;
  637.       if (cp[1] != '.')
  638.     goto lose;
  639.  
  640.       cp += 2;
  641.  
  642.       /* Check the width specification.  */
  643.       if ('0' <= *cp && *cp <= '9')
  644.     for (width = 0; (*cp >= '0' && *cp <= '9'); cp++)
  645.       width = (width * 10) + (*cp - '0');
  646.  
  647.       if (*cp != 'e' && *cp != 'f' && *cp != 'g')
  648.     goto lose;
  649.  
  650.       /* A precision of zero is valid for %f; everything else requires
  651.      at least one.  Width may be omitted anywhere.  */
  652.       if (width != -1
  653.       && (width < (*cp != 'f')
  654.           || width > DBL_DIG))
  655.     goto lose;
  656.  
  657.       if (cp[1] != 0)
  658.     goto lose;
  659.  
  660.       sprintf (buf, XSTRING (Vfloat_output_format)->data, data);
  661.     }
  662.  
  663.   /* Make sure there is a decimal point with digit after, or an
  664.      exponent, so that the value is readable as a float.  But don't do
  665.      this with "%.0f"; it's legal for that not to produce a decimal
  666.      point.  */
  667.   if (*cp != 'f' || width != 0)
  668.     {
  669.       for (cp = buf; *cp; cp++)
  670.     if ((*cp < '0' || *cp > '9') && *cp != '-')
  671.       break;
  672.  
  673.       if (*cp == '.' && cp[1] == 0)
  674.     {
  675.       cp[1] = '0';
  676.       cp[2] = 0;
  677.     }
  678.  
  679.       if (*cp == 0)
  680.     {
  681.       *cp++ = '.';
  682.       *cp++ = '0';
  683.       *cp++ = 0;
  684.     }
  685.     }
  686. }
  687. #endif /* LISP_FLOAT_TYPE */
  688.  
  689. static void
  690. print (obj, printcharfun, escapeflag)
  691.      Lisp_Object obj;
  692.      register Lisp_Object printcharfun;
  693.      int escapeflag;
  694. {
  695.   char buf[30];
  696.  
  697.   QUIT;
  698.  
  699. #if 1  /* I'm not sure this is really worth doing.  */
  700.   /* Detect circularities and truncate them.
  701.      No need to offer any alternative--this is better than an error.  */
  702.   if (XTYPE (obj) == Lisp_Cons || XTYPE (obj) == Lisp_Vector
  703.       || XTYPE (obj) == Lisp_Compiled)
  704.     {
  705.       int i;
  706.       for (i = 0; i < print_depth; i++)
  707.     if (EQ (obj, being_printed[i]))
  708.       {
  709.         sprintf (buf, "#%d", i);
  710.         strout (buf, -1, printcharfun);
  711.         return;
  712.       }
  713.     }
  714. #endif
  715.  
  716.   being_printed[print_depth] = obj;
  717.   print_depth++;
  718.  
  719.   if (print_depth > PRINT_CIRCLE)
  720.     error ("Apparently circular structure being printed");
  721. #ifdef MAX_PRINT_CHARS
  722.   if (max_print && print_chars > max_print)
  723.     {
  724.       PRINTCHAR ('\n');
  725.       print_chars = 0;
  726.     }
  727. #endif /* MAX_PRINT_CHARS */
  728.  
  729. #ifdef SWITCH_ENUM_BUG
  730.   switch ((int) XTYPE (obj))
  731. #else
  732.   switch (XTYPE (obj))
  733. #endif
  734.     {
  735.     default:
  736.       /* We're in trouble if this happens!
  737.      Probably should just abort () */
  738.       strout ("#<EMACS BUG: INVALID DATATYPE ", -1, printcharfun);
  739.       sprintf (buf, "(#o%3o)", (int) XTYPE (obj));
  740.       strout (buf, -1, printcharfun);
  741.       strout (" Save your buffers immediately and please report this bug>",
  742.           -1, printcharfun);
  743.       break;
  744.  
  745. #ifdef LISP_FLOAT_TYPE
  746.     case Lisp_Float:
  747.       {
  748.     char pigbuf[350];    /* see comments in float_to_string */
  749.  
  750.     float_to_string (pigbuf, XFLOAT(obj)->data);
  751.     strout (pigbuf, -1, printcharfun);
  752.       }
  753.       break;
  754. #endif /* LISP_FLOAT_TYPE */
  755.  
  756.     case Lisp_Int:
  757.       sprintf (buf, "%d", XINT (obj));
  758.       strout (buf, -1, printcharfun);
  759.       break;
  760.  
  761.     case Lisp_String:
  762.       if (!escapeflag)
  763.     print_string (obj, printcharfun);
  764.       else
  765.     {
  766.       register int i;
  767.       register unsigned char c;
  768.       struct gcpro gcpro1;
  769.  
  770.       GCPRO1 (obj);
  771.  
  772. #ifdef USE_TEXT_PROPERTIES
  773.       if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
  774.         {
  775.           PRINTCHAR ('#');
  776.           PRINTCHAR ('(');
  777.         }
  778. #endif
  779.  
  780.       PRINTCHAR ('\"');
  781.       for (i = 0; i < XSTRING (obj)->size; i++)
  782.         {
  783.           QUIT;
  784.           c = XSTRING (obj)->data[i];
  785.           if (c == '\n' && print_escape_newlines)
  786.         {
  787.           PRINTCHAR ('\\');
  788.           PRINTCHAR ('n');
  789.         }
  790.           else
  791.         {
  792.           if (c == '\"' || c == '\\')
  793.             PRINTCHAR ('\\');
  794.           PRINTCHAR (c);
  795.         }
  796.         }
  797.       PRINTCHAR ('\"');
  798.  
  799. #ifdef USE_TEXT_PROPERTIES
  800.       if (!NULL_INTERVAL_P (XSTRING (obj)->intervals))
  801.         {
  802.           traverse_intervals (XSTRING (obj)->intervals,
  803.                   0, 0, print_interval, printcharfun);
  804.           PRINTCHAR (')');
  805.         }
  806. #endif
  807.  
  808.       UNGCPRO;
  809.     }
  810.       break;
  811.  
  812.     case Lisp_Symbol:
  813.       {
  814.     register int confusing;
  815.     register unsigned char *p = XSYMBOL (obj)->name->data;
  816.     register unsigned char *end = p + XSYMBOL (obj)->name->size;
  817.     register unsigned char c;
  818.  
  819.     if (p != end && (*p == '-' || *p == '+')) p++;
  820.         if (p == end)
  821.       confusing = 0;
  822.     else
  823.       {
  824.         while (p != end && *p >= '0' && *p <= '9')
  825.           p++;
  826.         confusing = (end == p);
  827.       }
  828.  
  829.     p = XSYMBOL (obj)->name->data;
  830.     while (p != end)
  831.       {
  832.         QUIT;
  833.         c = *p++;
  834.         if (escapeflag)
  835.           {
  836.         if (c == '\"' || c == '\\' || c == '\'' || c == ';' || c == '#' ||
  837.             c == '(' || c == ')' || c == ',' || c =='.' || c == '`' ||
  838.             c == '[' || c == ']' || c == '?' || c <= 040 || confusing)
  839.           PRINTCHAR ('\\'), confusing = 0;
  840.           }
  841.         PRINTCHAR (c);
  842.       }
  843.       }
  844.       break;
  845.  
  846.     case Lisp_Cons:
  847.       /* If deeper than spec'd depth, print placeholder.  */
  848.       if (XTYPE (Vprint_level) == Lisp_Int
  849.       && print_depth > XINT (Vprint_level))
  850.     {
  851.       strout ("...", -1, printcharfun);
  852.       break;
  853.     }
  854.  
  855.       PRINTCHAR ('(');
  856.       {
  857.     register int i = 0;
  858.     register int max = 0;
  859.  
  860.     if (XTYPE (Vprint_length) == Lisp_Int)
  861.       max = XINT (Vprint_length);
  862.     /* Could recognize circularities in cdrs here,
  863.        but that would make printing of long lists quadratic.
  864.        It's not worth doing.  */
  865.     while (CONSP (obj))
  866.       {
  867.         if (i++)
  868.           PRINTCHAR (' ');
  869.         if (max && i > max)
  870.           {
  871.         strout ("...", 3, printcharfun);
  872.         break;
  873.           }
  874.         print (Fcar (obj), printcharfun, escapeflag);
  875.         obj = Fcdr (obj);
  876.       }
  877.       }
  878.       if (!NILP (obj) && !CONSP (obj))
  879.     {
  880.       strout (" . ", 3, printcharfun);
  881.       print (obj, printcharfun, escapeflag);
  882.     }
  883.       PRINTCHAR (')');
  884.       break;
  885.  
  886.     case Lisp_Compiled:
  887.       strout ("#", -1, printcharfun);
  888.     case Lisp_Vector:
  889.       PRINTCHAR ('[');
  890.       {
  891.     register int i;
  892.     register Lisp_Object tem;
  893.     for (i = 0; i < XVECTOR (obj)->size; i++)
  894.       {
  895.         if (i) PRINTCHAR (' ');
  896.         tem = XVECTOR (obj)->contents[i];
  897.         print (tem, printcharfun, escapeflag);
  898.       }
  899.       }
  900.       PRINTCHAR (']');
  901.       break;
  902.  
  903. #ifndef standalone
  904.     case Lisp_Buffer:
  905.       if (NILP (XBUFFER (obj)->name))
  906.     strout ("#<killed buffer>", -1, printcharfun);
  907.       else if (escapeflag)
  908.     {
  909.       strout ("#<buffer ", -1, printcharfun);
  910.       print_string (XBUFFER (obj)->name, printcharfun);
  911.       PRINTCHAR ('>');
  912.     }
  913.       else
  914.     print_string (XBUFFER (obj)->name, printcharfun);
  915.       break;
  916.  
  917.     case Lisp_Process:
  918.       if (escapeflag)
  919.     {
  920.       strout ("#<process ", -1, printcharfun);
  921.       print_string (XPROCESS (obj)->name, printcharfun);
  922.       PRINTCHAR ('>');
  923.     }
  924.       else
  925.     print_string (XPROCESS (obj)->name, printcharfun);
  926.       break;
  927.  
  928.     case Lisp_Window:
  929.       strout ("#<window ", -1, printcharfun);
  930.       sprintf (buf, "%d", XFASTINT (XWINDOW (obj)->sequence_number));
  931.       strout (buf, -1, printcharfun);
  932.       if (!NILP (XWINDOW (obj)->buffer))
  933.     {
  934.       strout (" on ", -1, printcharfun);
  935.       print_string (XBUFFER (XWINDOW (obj)->buffer)->name, printcharfun);
  936.     }
  937.       PRINTCHAR ('>');
  938.       break;
  939.  
  940.     case Lisp_Window_Configuration:
  941.       strout ("#<window-configuration>", -1, printcharfun);
  942.       break;
  943.  
  944. #ifdef MULTI_FRAME
  945.     case Lisp_Frame:
  946.       strout ((FRAME_LIVE_P (XFRAME (obj))
  947.            ? "#<frame " : "#<dead frame "),
  948.           -1, printcharfun);
  949.       print_string (XFRAME (obj)->name, printcharfun);
  950.       sprintf (buf, " 0x%x", (unsigned int) (XFRAME (obj)));
  951.       strout (buf, -1, printcharfun);
  952.       strout (">", -1, printcharfun);
  953.       break;
  954. #endif /* MULTI_FRAME */
  955.  
  956.     case Lisp_Marker:
  957.       strout ("#<marker ", -1, printcharfun);
  958.       if (!(XMARKER (obj)->buffer))
  959.     strout ("in no buffer", -1, printcharfun);
  960.       else
  961.     {
  962.       sprintf (buf, "at %d", marker_position (obj));
  963.       strout (buf, -1, printcharfun);
  964.       strout (" in ", -1, printcharfun);
  965.       print_string (XMARKER (obj)->buffer->name, printcharfun);
  966.     }
  967.       PRINTCHAR ('>');
  968.       break;
  969.  
  970.     case Lisp_Overlay:
  971.       strout ("#<overlay ", -1, printcharfun);
  972.       if (!(XMARKER (OVERLAY_START (obj))->buffer))
  973.     strout ("in no buffer", -1, printcharfun);
  974.       else
  975.     {
  976.       sprintf (buf, "from %d to %d in ",
  977.            marker_position (OVERLAY_START (obj)),
  978.            marker_position (OVERLAY_END   (obj)));
  979.       strout (buf, -1, printcharfun);
  980.       print_string (XMARKER (OVERLAY_START (obj))->buffer->name,
  981.             printcharfun);
  982.     }
  983.       PRINTCHAR ('>');
  984.       break;
  985.  
  986. #endif /* standalone */
  987.  
  988.     case Lisp_Subr:
  989.       strout ("#<subr ", -1, printcharfun);
  990.       strout (XSUBR (obj)->symbol_name, -1, printcharfun);
  991.       PRINTCHAR ('>');
  992.       break;
  993.     }
  994.  
  995.   print_depth--;
  996. }
  997.  
  998. #ifdef USE_TEXT_PROPERTIES
  999.  
  1000. /* Print a description of INTERVAL using PRINTCHARFUN.
  1001.    This is part of printing a string that has text properties.  */
  1002.  
  1003. void
  1004. print_interval (interval, printcharfun)
  1005.      INTERVAL interval;
  1006.      Lisp_Object printcharfun;
  1007. {
  1008.   PRINTCHAR (' ');
  1009.   print (make_number (interval->position), printcharfun, 1);
  1010.   PRINTCHAR (' ');
  1011.   print (make_number (interval->position + LENGTH (interval)),
  1012.      printcharfun, 1);
  1013.   PRINTCHAR (' ');
  1014.   print (interval->plist, printcharfun, 1);
  1015. }
  1016.  
  1017. #endif /* USE_TEXT_PROPERTIES */
  1018.  
  1019. void
  1020. syms_of_print ()
  1021. {
  1022.   staticpro (&Qprint_escape_newlines);
  1023.   Qprint_escape_newlines = intern ("print-escape-newlines");
  1024.  
  1025.   DEFVAR_LISP ("standard-output", &Vstandard_output,
  1026.     "Output stream `print' uses by default for outputting a character.\n\
  1027. This may be any function of one argument.\n\
  1028. It may also be a buffer (output is inserted before point)\n\
  1029. or a marker (output is inserted and the marker is advanced)\n\
  1030. or the symbol t (output appears in the minibuffer line).");
  1031.   Vstandard_output = Qt;
  1032.   Qstandard_output = intern ("standard-output");
  1033.   staticpro (&Qstandard_output);
  1034.  
  1035. #ifdef LISP_FLOAT_TYPE
  1036.   DEFVAR_LISP ("float-output-format", &Vfloat_output_format,
  1037.     "The format descriptor string used to print floats.\n\
  1038. This is a %-spec like those accepted by `printf' in C,\n\
  1039. but with some restrictions.  It must start with the two characters `%.'.\n\
  1040. After that comes an integer precision specification,\n\
  1041. and then a letter which controls the format.\n\
  1042. The letters allowed are `e', `f' and `g'.\n\
  1043. Use `e' for exponential notation \"DIG.DIGITSeEXPT\"\n\
  1044. Use `f' for decimal point notation \"DIGITS.DIGITS\".\n\
  1045. Use `g' to choose the shorter of those two formats for the number at hand.\n\
  1046. The precision in any of these cases is the number of digits following\n\
  1047. the decimal point.  With `f', a precision of 0 means to omit the\n\
  1048. decimal point.  0 is not allowed with `e' or `g'.\n\n\
  1049. A value of nil means to use `%.20g'.");
  1050.   Vfloat_output_format = Qnil;
  1051.   Qfloat_output_format = intern ("float-output-format");
  1052.   staticpro (&Qfloat_output_format);
  1053. #endif /* LISP_FLOAT_TYPE */
  1054.  
  1055.   DEFVAR_LISP ("print-length", &Vprint_length,
  1056.     "Maximum length of list to print before abbreviating.\n\
  1057. A value of nil means no limit.");
  1058.   Vprint_length = Qnil;
  1059.  
  1060.   DEFVAR_LISP ("print-level", &Vprint_level,
  1061.     "Maximum depth of list nesting to print before abbreviating.\n\
  1062. A value of nil means no limit.");
  1063.   Vprint_level = Qnil;
  1064.  
  1065.   DEFVAR_BOOL ("print-escape-newlines", &print_escape_newlines,
  1066.     "Non-nil means print newlines in strings as backslash-n.");
  1067.   print_escape_newlines = 0;
  1068.  
  1069.   /* prin1_to_string_buffer initialized in init_buffer_once in buffer.c */
  1070.   staticpro (&Vprin1_to_string_buffer);
  1071.  
  1072.   defsubr (&Sprin1);
  1073.   defsubr (&Sprin1_to_string);
  1074.   defsubr (&Sprinc);
  1075.   defsubr (&Sprint);
  1076.   defsubr (&Sterpri);
  1077.   defsubr (&Swrite_char);
  1078.   defsubr (&Sexternal_debugging_output);
  1079.  
  1080.   Qexternal_debugging_output = intern ("external-debugging-output");
  1081.   staticpro (&Qexternal_debugging_output);
  1082.  
  1083. #ifndef standalone
  1084.   defsubr (&Swith_output_to_temp_buffer);
  1085. #endif /* not standalone */
  1086. }
  1087.